﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Sharp.Portal
{
    /// <summary>
    /// 拉姆达表达式
    /// </summary>
    public static class LambdaHelper
    {
        #region 找出属性路径

        /// <summary>
        /// 找出属性路径
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool TryParsePath(Expression expression, out string path)
        {
            path = null;
            Expression expression2 = expression.RemoveConvert();
            MemberExpression expression3 = expression2 as MemberExpression;
            MethodCallExpression expression4 = expression2 as MethodCallExpression;
            if (expression3 != null)
            {
                string str2;
                string name = expression3.Member.Name;
                if (!TryParsePath(expression3.Expression, out str2))
                {
                    return false;
                }
                path = (str2 == null) ? name : (str2 + "." + name);
            }
            else if (expression4 != null)
            {
                if ((expression4.Method.Name == "Select") && (expression4.Arguments.Count == 2))
                {
                    string str3;
                    if (!TryParsePath(expression4.Arguments[0], out str3))
                    {
                        return false;
                    }
                    if (str3 != null)
                    {
                        LambdaExpression expression5 = expression4.Arguments[1] as LambdaExpression;
                        if (expression5 != null)
                        {
                            string str4;
                            if (!TryParsePath(expression5.Body, out str4))
                            {
                                return false;
                            }
                            if (str4 != null)
                            {
                                path = str3 + "." + str4;
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
            return true;
        }

        #endregion

        #region 找出属性的标题

        /// <summary>
        /// 找出属性的标题
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="displayName"></param>
        /// <returns></returns>
        public static bool TryParseDisplay(Expression expression, out string displayName)
        {
            displayName = null;
            Expression expression2 = expression.RemoveConvert();
            MemberExpression expression3 = expression2 as MemberExpression;
            MethodCallExpression expression4 = expression2 as MethodCallExpression;
            if (expression3 != null)
            {
                string str2;
                string name = expression3.Member.Name;
                var displayAttribute =
                    expression3.Expression.Type.GetProperty(name)
                        .GetCustomAttributes(typeof(DisplayAttribute), true)
                        .FirstOrDefault() as DisplayAttribute;

                if (displayAttribute != null)
                {
                    str2 = displayAttribute.Name;
                }
                else
                {
                    return false;
                }
                displayName = str2;
            }
            else if (expression4 != null)
            {
                if ((expression4.Method.Name == "Select") && (expression4.Arguments.Count == 2))
                {
                    string str3;
                    if (!TryParsePath(expression4.Arguments[0], out str3))
                    {
                        return false;
                    }
                    if (str3 != null)
                    {
                        LambdaExpression expression5 = expression4.Arguments[1] as LambdaExpression;
                        if (expression5 != null)
                        {
                            string str4;
                            if (!TryParsePath(expression5.Body, out str4))
                            {
                                return false;
                            }
                            if (str4 != null)
                            {
                                displayName = str3 + "." + str4;
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
            return true;
        }

        #endregion

        #region 找出属性的代码项

        /// <summary>
        /// 找出属性的代码项
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="codeSort"></param>
        /// <returns></returns>
        public static bool TryParseCodeSort(Expression expression, out string codeSort)
        {
            codeSort = null;
            Expression expression2 = expression.RemoveConvert();
            MemberExpression expression3 = expression2 as MemberExpression;
            MethodCallExpression expression4 = expression2 as MethodCallExpression;
            if (expression3 != null)
            {
                string str2;
                string name = expression3.Member.Name;
                var codeSortAttribute =
                    expression3.Expression.Type.GetProperty(name)
                        .GetCustomAttributes(typeof(MasterDataAttribute), true)
                        .FirstOrDefault() as MasterDataAttribute;

                if (codeSortAttribute != null)
                {
                    str2 = codeSortAttribute.Category;
                }
                else
                {
                    return false;
                }
                codeSort = str2;
            }

            return true;
        }

        #endregion
        /// <summary>
        /// 移除转换
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static Expression RemoveConvert(this Expression expression)
        {
            while ((expression.NodeType == ExpressionType.Convert) ||
                   (expression.NodeType == ExpressionType.ConvertChecked))
            {
                expression = ((UnaryExpression)expression).Operand;
            }
            return expression;
        }
        

    }
}